#include "mainwindow.h"
#include "ui_mainwindow.h"


#include <QDebug>
#include <QFileDialog>
#include <QThread>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(this->geometry().size());
    this->setWindowTitle("Linus Player");

    play_status = STOP;
    first_frame = true;

    // the play function
    auto play_func = std::function<void ()> ([&]() {
        int ret = -1;
        unsigned char *data = (unsigned char *)malloc(MAX_BUF_SIZE);
        int len = -1;

        PlayMode playMode = ONE_SCREEN;
        std::string fileName;

        CFFmpeg *ffmpeg = nullptr;
        CSDL2 *sdl = nullptr;

        while(1)
        {
            switch (play_status)
            {
                case START:
                {
                    if (nullptr == ffmpeg && nullptr == sdl)
                    {
                        if(file_path.isEmpty())
                        {
                            play_status = STOP;
                            qDebug()<<"file_path.isEmpty";
                            break;
                        }

                        fileName = file_path.toUtf8().data();
                        ffmpeg = new CFFmpeg(fileName);

                        sdl = new CSDL2;
                        sdl->initSDL2(ffmpeg->get_width(), ffmpeg->get_height(), playMode, ffmpeg->get_fps(), (void *)(ui->label->winId()));
                        qDebug()<<"new ffmpeg sdl";
                    }

                    CSDL2::set_play_status(true);
                    ret = ffmpeg->FFmpeg_read_frame(data, len);
                    if (0 == ret)
                    {
                        if (sdl->playFrame(data) < 0)
                        {
                            play_status = STOP;
                            break;
                        }

                        // In order to display the first frame when open the video file
                        if (first_frame)
                        {
                            first_frame = false;
                            play_status = PAUSE;
                            CSDL2::set_play_status(false);
                            qDebug()<<"first frame start ===========";
                        }
                    }
                    else if (ret < 0)
                    {
                        play_status = STOP;
                        break;
                    }
                    break;
                }
                case PAUSE:
                {
                    thread->sleep(1);
                    CSDL2::set_play_status(false);
                    break;
                }
                case STOP:
                {
                    if (nullptr != ffmpeg && nullptr != sdl)
                    {
                        delete ffmpeg;
                        ffmpeg = nullptr;

                        delete sdl;
                        sdl = nullptr;
                        qDebug()<<"stop free ffmpeg sdl";
                        CSDL2::set_play_status(false);
                    }

                    thread->sleep(1);
                    break;
                }
            }
        }
        delete data;
    });

    // run the play thread
    thread = QThread::create(play_func);
    thread->setParent(this);
    thread->start();
}

MainWindow::~MainWindow()
{
    thread->wait();
    thread->quit();
    delete thread;
    delete ui;
}


void MainWindow::on_pushButtonPlay_clicked()
{
    play_status = START;
    CSDL2::clear_play_event();
    qDebug()<<"PLAY ===========";
}

void MainWindow::on_pushButtonPause_clicked()
{
    play_status = PAUSE;
    qDebug()<<"PAUSE ===========";
}

void MainWindow::on_pushButtonStop_clicked()
{
    play_status = STOP;
    qDebug()<<"STOP ===========";
}

void MainWindow::on_pushButtonOpen_clicked()
{
    play_status = STOP;

    file_path = QFileDialog::getOpenFileName(
                this,
                "open",
                "../",
                "all(*.*)");

    first_frame = true;
    play_status = START;
    qDebug()<<"OPEN FILE ===========";
}
